package com.ruoyi.bole.wechatSubscribe.controller;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.bole.wechatSubscribe.domain.BoleWechatSubscribeUser;
import com.ruoyi.bole.wechatSubscribe.service.IBoleWechatSubscribeUserService;
import com.ruoyi.bole.wechatSubscribe.service.WxSubscribeService;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.Map;

@RestController
public class WxSubscribeController {
    @Autowired
    private WxSubscribeService wxSubscribeService;

    @Autowired
    private IBoleWechatSubscribeUserService iBoleWechatSubscribeUserService;
    //同意订阅
    @PostMapping("/bole/agreeSubscribe")
    public AjaxResult agreeSubscribe(@RequestBody Map<String, Object> requestData) {
        String url = "https://api.weixin.qq.com/sns/jscode2session?appid={0}&secret={1}&js_code={2}&grant_type=authorization_code";
        String replaceUrl = url.replace("{0}", "wx1ce0d323ecff9862").replace("{1}", "981546ad863f53d6aceba691bb76b0b8").replace("{2}", requestData.get("code").toString()); //替换为需要的值
        JSONObject jsonObject = JSONObject.parseObject(HttpUtils.sendGet(replaceUrl));    //转为json格式接收
//请求成功后 获取数据
        String openid = jsonObject.getString("openid");

        BoleWechatSubscribeUser user = new BoleWechatSubscribeUser();
        //先查有没有 有就不用管
        BoleWechatSubscribeUser userByOpenid = iBoleWechatSubscribeUserService.selectBoleWechatSubscribeUserByUserId(SecurityUtils.getUserId());
        if(userByOpenid != null){
            userByOpenid.setSubscribeStatus(3L);
            iBoleWechatSubscribeUserService.updateBoleWechatSubscribeUser(userByOpenid);
            return AjaxResult.success("订阅成功");
        }
        user.setOpenid(openid);
        user.setSubscribeTime(new Date());
        user.setTemplateId("nOBpWMAyW_l91hC1YmG_9Bme8zgEYBvEl8d7e0Con7w");
        user.setUserId(SecurityUtils.getUserId());
        user.setRemark("订阅消息 发送完删除");
        iBoleWechatSubscribeUserService.insertBoleWechatSubscribeUser(user);
        return AjaxResult.success("订阅成功");
    }
    /**   下面这个是测试的 可以删
     * 发送订阅消息
     * @param requestData 请求数据
     * @return 结果
     */
    @PostMapping("/bole/sendSubscribeMessage")
    public AjaxResult sendSubscribeMessage(@RequestBody Map<String, Object> requestData) {
        String url = "https://api.weixin.qq.com/sns/jscode2session?appid={0}&secret={1}&js_code={2}&grant_type=authorization_code";
        String replaceUrl = url.replace("{0}", "wx1ce0d323ecff9862").replace("{1}", "981546ad863f53d6aceba691bb76b0b8").replace("{2}", requestData.get("code").toString()); //替换为需要的值
        JSONObject jsonObject = JSONObject.parseObject(HttpUtils.sendGet(replaceUrl));    //转为json格式接收
//请求成功后 获取数据
        String openid = jsonObject.getString("openid");

//String openid = requestData.get("openid").toString();ogMep7QSoj4scS9cANYvW
        System.out.println(openid);
        System.out.println(requestData);
        String templateId = (String) requestData.get("templateId");
        Map<String, String> data = (Map<String, String>) requestData.get("data");
        return wxSubscribeService.sendSubscribeMessage(openid, templateId, data);
    }
}